import sys

from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QToolBox, QStyle, QDialog, \
    QTextEdit

"""
    折叠选项卡
"""


class MyWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        # 用来存储选项卡下属的widget集合
        self.widgetList: list = []

        # 切换打开关闭的图标样式
        self.iconArrowRight = self.style().standardPixmap(QStyle.StandardPixmap.SP_ArrowRight)
        self.iconArrowDown = self.style().standardPixmap(QStyle.StandardPixmap.SP_ArrowDown)

        # DOC: 1. 创建ToolBox
        self.toolBox = QToolBox()

        self.toolBox.setStyleSheet(
            """
                QToolBox::tab {
                    /* 文本居左 */
                    text-align: left;
                    /* 图标居右 */
                    padding-right: 20px;
                    background-position: right center;
                }
                
                QToolBoxButton {
                    /* 确保按钮内容对齐 */
                    text-align: left;
                    padding-left: 10px;
                }
            """
        )

        for i in range(3):
            _widget = self.addWidget()
            # DOC: 2. 给toolBox添加选项卡
            self.toolBox.addItem(_widget, self.iconArrowRight, f'选项卡{len(self.widgetList)}')

        # DOC: 3. 绑定选项卡切换信号 currentChanged
        self.toolBox.currentChanged.connect(self.currentChanged)
        self.currentChanged(0)  # 初始化箭头状态

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.toolBox)
        self.setLayout(self.layout)

    def currentChanged(self, index):
        # 先全部重置为未打卡，使用self.toolBox.count()获取toolbox的选项卡数量
        for i in range(self.toolBox.count()):
            self.toolBox.setItemIcon(i, self.iconArrowRight)
        self.toolBox.setItemIcon(index, self.iconArrowDown)

    def addWidget(self):
        widget = QWidget()
        widget_layout = QVBoxLayout()
        widget_layout.addWidget(QLabel(f'这是一个选项卡{len(self.widgetList) + 1}'))
        widget_layout.addWidget(QPushButton(f'按钮{len(self.widgetList) + 1}'))
        widget.setLayout(widget_layout)
        self.widgetList.append(SingleWidget(widget, widget_layout))
        return widget


class SingleWidget(object):
    """
    选项卡中单个widget的属性
    """
    widget = None
    widget_layout = None

    def __init__(self, widget, widget_layout):
        self.widget = widget
        self.widget_layout = widget_layout


class StyleEditor(QDialog):
    def __init__(self, target_widget):
        super().__init__()
        self.target_widget = target_widget
        self.setWindowTitle("样式表编辑器")

        self.editor = QTextEdit()
        self.editor.setPlainText(self.target_widget.styleSheet())

        apply_btn = QPushButton("应用样式")
        apply_btn.clicked.connect(self.apply_style)

        layout = QVBoxLayout()
        layout.addWidget(self.editor)
        layout.addWidget(apply_btn)
        self.setLayout(layout)

    def apply_style(self):
        self.target_widget.setStyleSheet(self.editor.toPlainText())




if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWidget()
    window.show()

    # 打开样式调节器
    style_editor = StyleEditor(window.toolBox)
    style_editor.show()

    app.exec()
